﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


namespace TestJQGrid
{ 
    public partial class TaxAgent : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fillAgents();
                fillStates();
                fillTax();
            }
        }

        public void fillAgents()
        {
            DataTable dt = new DataTable();
            dt = GlobalMethods.ExecuteSelectQuery("select AgentId,AgentName from Agents where Status='Approved' order by AgentName");

            DDAgents.DataSource = dt;
            DDAgents.DataBind();
        }

        public void fillStates()
        {
            DataTable dt = new DataTable();
            dt = GlobalMethods.ExecuteSelectQuery("select * from ValidValues where TableName='State' order by ColumnName");

            DDStates.DataSource = dt;
            DDStates.DataBind();            
        }

        public void fillTax()
        {
            DataTable dt = new DataTable();
            dt = GlobalMethods.ExecuteSelectQuery("select * from ValidValues where TableName='Taxes' order by ColumnName");
            DDTax.DataSource = dt;
            DDTax.DataBind();
        }

        protected void JQGrid1_DataRequesting(object sender, Trirand.Web.UI.WebControls.JQGridDataRequestEventArgs e)
        {
            DataTable dt = getTaxDetailsPerAgents();
            if (dt == null) return;

            JQGrid1.DataSource = dt;
            JQGrid1.DataBind();
        }

        public DataTable getTaxDetailsPerAgents()
        {
            string query = "select * from AgentTaxMaster order by AgentID";
            
            DataTable dt = new DataTable();
            dt = GlobalMethods.ExecuteSelectQuery(query);

            return dt;
        }

        protected void JQGrid1_OnRowAdding(object sender, Trirand.Web.UI.WebControls.JQGridRowAddEventArgs e)
        {
            string chkPreRecord = GlobalMethods.ExecuteScalar("select TaxType from AgentTaxMaster where State='" + e.RowData["State"].ToString() + "' and FromDate='" + Convert.ToDateTime(e.RowData["FromDate"].ToString()) + "' and ToDate='" + Convert.ToDateTime(e.RowData["ToDate"].ToString()) + "' and TaxType='" + e.RowData["TaxType"].ToString() + "'");

            if (!String.IsNullOrWhiteSpace(chkPreRecord)) return;
            
            string InsertQuery = "INSERT INTO AgentTaxMaster([AgentID],[State],[TaxType],[TaxPercent],[FromDate],[ToDate],[IsExclusive],[CreatedBy],[CreatedOn]) VALUES(";
            InsertQuery += Convert.ToInt32(e.RowData["AgentID"].ToString()) + ",";
            InsertQuery += " '" + e.RowData["State"].ToString() + "',";
            InsertQuery += " '" + e.RowData["TaxType"].ToString() + "',";
            InsertQuery += Convert.ToInt32(e.RowData["TaxPercent"].ToString()) + ",";
            InsertQuery += " '" + Convert.ToDateTime(e.RowData["FromDate"].ToString()) + "',";
            InsertQuery += " '" + Convert.ToDateTime(e.RowData["ToDate"].ToString()) + "',";
            InsertQuery += " '" + e.RowData["IsExclusive"].ToString() + "',";
            InsertQuery += " '" + Session["loginId"].ToString() + "',";
            InsertQuery += " '" + DateTime.Now.ToString() + "')";

            GlobalMethods.ExecuteNonQuery(InsertQuery);
            WebMsgBox.Show("Record added successfully.");
        }

        protected void JQGrid1_RowDeleting(object sender, Trirand.Web.UI.WebControls.JQGridRowDeleteEventArgs e)
        {
            GlobalMethods.ExecuteNonQuery("update AgentTaxMaster set ToDate='" + DateTime.Now.ToString() + "' where EntryID =" + Convert.ToInt32(e.RowKey));
            WebMsgBox.Show("Record deleted succesfully.");
        }

        protected void JQGrid1_OnRowEditing(object sender, Trirand.Web.UI.WebControls.JQGridRowEditEventArgs e)
        {
            string UpdateQuery = "UPDATE AgentTaxMaster SET ";
            UpdateQuery += " AgentID=" + Convert.ToInt32(e.RowData["AgentID"].ToString());
            UpdateQuery += " ,State='" + e.RowData["State"].ToString() + "'";
            UpdateQuery += " ,TaxType='" + e.RowData["TaxType"].ToString() + "'";
            UpdateQuery += " ,TaxPercent=" + Convert.ToDecimal(e.RowData["TaxPercent"].ToString());
            UpdateQuery += " ,FromDate='" + Convert.ToDateTime(e.RowData["FromDate"].ToString()) + "'";
            UpdateQuery += " ,ToDate='" + Convert.ToDateTime(e.RowData["ToDate"].ToString()) + "'";
            UpdateQuery += " ,IsExclusive='" + e.RowData["IsExclusive"].ToString() + "'";
            UpdateQuery += " WHERE EntryID=" + Convert.ToInt32(e.RowData["EntryID"].ToString()) + "";

            GlobalMethods.ExecuteNonQuery(UpdateQuery);
            WebMsgBox.Show("Record updated successfully.");
        }
    }
}